package ua.kpi_java_training.zabrodskyi.module3;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

/**
 * @author Maksym Zabrodskyi
 * @version 23-Jun-2013
 */
public class EnvironmentTest {

	Environment environment;

	@Before
	public void setUp() throws Exception {
		environment = new Environment(Settings.getWidth(), Settings.getHeight());
		environment.add(ElementType.BARRIER);
		environment.add(ElementType.PREDATOR);
		environment.add(ElementType.VICTIM);
		environment.add(ElementType.VICTIM);
	}

	@Test
	public void shouldAddElement() {
		environment.add(ElementType.BARRIER);
		environment.add(ElementType.PREDATOR);
		environment.add(ElementType.VICTIM);
		int actualCountElements = environment.getCountAllElements();
		assertEquals(7, actualCountElements);
	}

	@Test
	public void shouldRemoveElement() {
		Element element = environment.getElement(1);
		Cell cell = element.getCell();
		environment.remove(ElementType.PREDATOR, cell);
		int actualCountElements = environment.getCountAllElements();
		assertEquals(3, actualCountElements);
	}

	@Test
	public void shouldNotRemoveElement() {
		Cell cell = environment.getFreeCell();
		environment.remove(ElementType.PREDATOR, cell);
		int actualCountElements = environment.getCountAllElements();
		assertEquals(4, actualCountElements);
	}

	@Test
	public void shouldGetElement() {
		int indexElement = 0;
		Element element = environment.getElement(indexElement);
		ElementType expectedElementType = element.getType();
		ElementType actualElementType = ElementType.BARRIER;
		assertEquals(expectedElementType, actualElementType);
	}

	@Test
	public void shouldGetCountBarriers() {
		int actualCountBarriers = environment
				.getCountElements(ElementType.BARRIER);
		assertEquals(1, actualCountBarriers);
	}

	@Test
	public void shouldGetCountVictims() {
		int actualCountVictims = environment
				.getCountElements(ElementType.VICTIM);
		assertEquals(2, actualCountVictims);
	}

	@Test
	public void shouldGetCountAllElements() {
		environment.add(ElementType.BARRIER);
		int actualCountElements = environment.getCountAllElements();
		assertEquals(5, actualCountElements);
	}

	@Test
	public void shouldGetFreeCell() {
		Cell freeCell = environment.getFreeCell();
		assertNotNull(freeCell);
	}

	@Test
	public void shouldFreeCellInIndex() {
		int index = Settings.getWidth() + Settings.getHeight() - 1;
		assertTrue(environment.isFreeCell(index));
	}

	@Test
	public void shouldFreeCellInNewCell() {
		Cell newCell = environment.getFreeCell();
		assertTrue(environment.isFreeCell(newCell));
	}

	@Test
	public void shouldHasFreeCell() {
		assertTrue(environment.hasFreeCell());
	}

	@Test
	public void shouldNotHasFreeCell() {
		for (int counter = 0; counter < environment.getSize(); counter++) {
			environment.add(ElementType.BARRIER);
		}
		assertFalse(environment.hasFreeCell());
	}

	@Test
	public void shouldTrueIsCellSettleElement() {
		int indexElement = 0;
		Element expectedElement = environment.getElement(indexElement);
		Cell expectedCell = expectedElement.getCell();
		assertTrue(environment.isSettleElement(expectedElement.getType(),
				expectedCell));
	}

	@Test
	public void shouldGetSizeEnvironment() {
		environment = new Environment(5, 6);
		int expectedSize = 30;
		int actualSize = environment.getSize();
		assertEquals(expectedSize, actualSize);
	}

	@Test
	public void testEnvironmentToString() {
		environment = new Environment(4, 5);
		String expected = "Environment = 20 [4x5]";
		assertEquals(expected, environment.toString());
	}

}
